package manage

import (
	"lingc/models"
	"lingc/services"
	"lingc/utils" // 导入 common 包
	"strings"

	"github.com/gin-gonic/gin"
)

// PageAuthHandleLog 获取所有用户
func PageAuthHandleLog(c *gin.Context) {

	var dataRequest struct {
		Keywords string `json:"keywords" form:"keywords"`
		Page     int    `json:"page" form:"page"`
		PageSize int    `json:"pageSize" form:"pageSize"` // Exported field
	}
	if err := c.ShouldBind(&dataRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	where := ""
	if dataRequest.Keywords != "" {
		keyword := "'%" + dataRequest.Keywords + "%'"
		where += " (`name` like " + keyword + " or `param` like " + keyword + ")"
	}
	list, page, total, err := services.NewAuthHandleLogService(utils.MysqlClient).PageAuthHandleLogList(where, dataRequest.Page, dataRequest.PageSize)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"list": list, "page": page, "total": total})
}

// GetUser 获取用户
func GetAuthHandleLog(c *gin.Context) {

	var dataRequest struct {
		Keyword string `json:"keyword" form:"keyword"`
	}
	where := ""
	if err := c.ShouldBind(&dataRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	if dataRequest.Keyword != "" {
		keyword := "%" + dataRequest.Keyword + "%"
		where += " `name` like " + keyword
	}
	user, err := services.NewAuthHandleLogService(utils.MysqlClient).GetAuthHandleLog(where)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"data": user})
}

// CreateUser 创建用户
func CreateAuthHandleLog(c *gin.Context) {

	/**
	AdminID   uint   `json:"admin_id"`
	Name      string `json:"name"`
	URL       string `json:"url"`
	Param     string `json:"param"`
	AppID     int    `json:"app_id"`
	CreatedAt string `json:"created_at"`
	UpdatedAt string `json:"updated_at"`
	*/
	var createRequest struct {
		AdminID uint   `json:"admin_id" form:"admin_id"`
		Name    string `json:"name" form:"name"`
		URL     string `json:"url" form:"url"`
		Param   string `json:"param" form:"param"`
	}
	if err := c.ShouldBindJSON(&createRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	data := &models.AuthHandleLogCreate{
		AdminID: createRequest.AdminID,
		Name:    createRequest.Name,
		URL:     createRequest.URL,
		Param:   createRequest.Param,
	}
	data, err := services.NewAuthHandleLogService(utils.MysqlClient).CreateAuthHandleLog(data)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"data": data})
}

// UpdateUser 更新用户
func UpdateAuthHandleLog(c *gin.Context) {
	var createRequest struct {
		ID      uint   `json:"id" form:"id"`
		AdminID uint   `json:"admin_id" form:"admin_id"`
		Name    string `json:"name" form:"name"`
		URL     string `json:"url" form:"url"`
		Param   string `json:"param" form:"param"`
	}
	if err := c.ShouldBindJSON(&createRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	data := &models.AuthHandleLogUpdate{
		ID:      createRequest.ID,
		AdminID: createRequest.AdminID,
		Name:    createRequest.Name,
		URL:     createRequest.URL,
		Param:   createRequest.Param,
	}
	data, err := services.NewAuthHandleLogService(utils.MysqlClient).UpdateAuthHandleLog(data)
	if err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"data": data})
}

// DeleteUser 删除用户
func DeleteAuthHandleLog(c *gin.Context) {
	var createRequest struct {
		ID  uint   `json:"id" form:"id"`
		IDs string `json:"ids" form:"ids"`
	}
	if err := c.ShouldBindJSON(&createRequest); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	if createRequest.ID == 0 && len(createRequest.IDs) == 0 {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	if createRequest.ID != 0 {
		if err := services.NewAuthHandleLogService(utils.MysqlClient).DeleteAuthHandleLog(createRequest.ID); err != nil {
			utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
			return
		}
	}
	if len(createRequest.IDs) == 0 {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "Invalid request"})
		return
	}
	idStrs := strings.Split(createRequest.IDs, ",")
	var ids []string
	for _, idStr := range idStrs {
		idStr = strings.TrimSpace(idStr)
		if idStr == "" {
			continue
		}
		ids = append(ids, idStr)
	}
	// Convert IDs slice to a format suitable for batch deletion
	if len(ids) == 0 {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": "No valid IDs provided"})
		return
	}
	if err := services.NewAuthHandleLogService(utils.MysqlClient).DeleteAuthHandleLogs(ids); err != nil {
		utils.SendJSONResponse(c, utils.ErrorCode, gin.H{"msg": err.Error()})
		return
	}
	// ... 这里添加删除用户的逻辑 ...
	utils.SendJSONResponse(c, utils.SuccessCode, gin.H{"msg": "Delete user"})
}
